package design.patterns.structural.proxy.dynamicproxy.jdk;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/4/4 9:04
 */
public class Test {
    public static void main(String[] args) {
        //创建被代理的对象
        People people = new People();
        //创建与代理对象相关联的invocationHandler
        InvocationHandler handler = new LawyerInvocationHandler<People>(people);
        //创建代理对象
        Activity activity = (Activity) Proxy.newProxyInstance(Activity.class.getClassLoader(), new Class[]{Activity.class}, handler);

        activity.prosecute();

    }
}

class Test2{
    public static void main(String[] args) {
        byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy0", People.class.getInterfaces());
        String path = "D:/ActivityProxy.class";
        try(FileOutputStream fos = new FileOutputStream(path)) {
            fos.write(classFile);
            fos.flush();
            System.out.println("代理类class文件写入成功");
        } catch (Exception e) {
            System.out.println("写文件错误");
        }
    }
}